<?php
require_once 'config.php';

session_start();

// 验证管理员权限
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header("Location: index.html");
    exit();
}

$conn = getDBConnection();

// 处理添加部门
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_department'])) {
    $department = trim($_POST['department']);
    $username = trim($_POST['username']);
    $password = password_hash(trim($_POST['password']), PASSWORD_DEFAULT);
    
    $stmt = $conn->prepare("INSERT INTO users (username, password, department, role) VALUES (?, ?, ?, 'department')");
    $stmt->bind_param("sss", $username, $password, $department);
    $stmt->execute();
}

// 处理删除部门
if (isset($_GET['delete_department'])) {
    $department = $_GET['delete_department'];
    
    // 删除部门用户
    $stmt = $conn->prepare("DELETE FROM users WHERE department = ? AND role = 'department'");
    $stmt->bind_param("s", $department);
    $stmt->execute();
    
    // 删除部门人员
    $stmt = $conn->prepare("DELETE FROM personnel WHERE department = ?");
    $stmt->bind_param("s", $department);
    $stmt->execute();
}

// 获取所有部门
$departments = $conn->query("SELECT DISTINCT department FROM users WHERE role = 'department' ORDER BY department");

// 获取各部门人数统计
$stats = $conn->query("SELECT department, COUNT(*) as count FROM personnel GROUP BY department");
$department_counts = [];
while ($row = $stats->fetch_assoc()) {
    $department_counts[$row['department']] = $row['count'];
}

// 获取总人数
$total = $conn->query("SELECT COUNT(*) as total FROM personnel")->fetch_assoc()['total'];

// 获取各部门状态统计
$status_stats = $conn->query("SELECT department, status, COUNT(*) as count FROM personnel GROUP BY department, status");
$department_status_counts = [];
while ($row = $status_stats->fetch_assoc()) {
    $department_status_counts[$row['department']][$row['status']] = $row['count'];
}

$conn->close();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员面板</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="admin-container">
        <h1>管理员面板</h1>
        
        <div class="stats-panel">
            <h2>人员统计</h2>
            <p>系统总人数: <?php echo $total; ?></p>
            <table>
                <tr><th>部门</th><th>人数</th></tr>
                <?php while ($row = $departments->fetch_assoc()): ?>
                <tr>
                    <td><?php echo $row['department']; ?></td>
                    <td><?php echo $department_counts[$row['department']] ?? 0; ?></td>
                </tr>
                <?php endwhile; ?>
            </table>
        </div>
        
        <div class="department-management">
            <h2>部门管理</h2>
            <form method="post">
                <div class="form-group">
                    <label for="department">部门名称</label>
                    <input type="text" id="department" name="department" required>
                </div>
                <div class="form-group">
                    <label for="username">部门账号</label>
                    <input type="text" id="username" name="username" required>
                </div>
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" id="password" name="password" required>
                </div>
                <button type="submit" name="add_department" class="btn">添加部门</button>
            </form>
            
            <h3>现有部门</h3>
            <ul>
                <?php $departments->data_seek(0); while ($row = $departments->fetch_assoc()): ?>
                <li>
                    <?php echo $row['department']; ?>
                    <a href="?delete_department=<?php echo urlencode($row['department']); ?>" class="delete-btn">删除</a>
                </li>
                <?php endwhile; ?>
            </ul>
        </div>
        
        <div class="status-panel">
            <h2>人员状态统计</h2>
            <table>
                <tr>
                    <th>部门</th>
                    <th>在位</th>
                    <th>休假</th>
                    <th>公差勤务</th>
                    <th>病假</th>
                    <th>事假</th>
                    <th>内部借调</th>
                    <th>不在位</th>
                </tr>
                <?php $departments->data_seek(0); while ($row = $departments->fetch_assoc()): ?>
                <tr>
                    <td><?php echo $row['department']; ?></td>
                    <td><?php echo $department_status_counts[$row['department']]['在位'] ?? 0; ?></td>
                    <td><?php echo $department_status_counts[$row['department']]['休假'] ?? 0; ?></td>
                    <td><?php echo $department_status_counts[$row['department']]['公差勤务'] ?? 0; ?></td>
                    <td><?php echo $department_status_counts[$row['department']]['病假'] ?? 0; ?></td>
                    <td><?php echo $department_status_counts[$row['department']]['事假'] ?? 0; ?></td>
                    <td><?php echo $department_status_counts[$row['department']]['内部借调'] ?? 0; ?></td>
                    <td><?php echo $department_status_counts[$row['department']]['不在位'] ?? 0; ?></td>
                </tr>
                <?php endwhile; ?>
            </table>
        </div>
        
        <div class="navigation">
            <a href="logout.php" class="logout-btn">退出登录</a>
        </div>
    </div>
</body>
</html>